package com.ds.infrastructure.hyperspace.console.utils;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author: yaozhendong
 * @create: 2019-10-31 15:02
 **/

public class StreamUtil {
    private StreamUtil() {

    }

    /**
     * 通过KEY做排重
     * eg:tbDeployNodeList.stream().filter(distinctByKey(TbDeployNode::getHost)).collect(Collectors.toList());
     *
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}
